package org.ainlolcat.ainscience.templates;

import org.ainlolcat.ainscience.matrix.Consts;
import org.ainlolcat.ainscience.tools;
import org.jdom.Document;
import org.jdom.Element;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.UUID;

/**
 * @author ainlolcat
 * Date: 10/25/11
 */
public class Template {
    /*
    * mmpkeys allow simple configuration of parsing matrix parser parameters
    */
    private static Hashtable<String,Integer> mppkeys;

    String filename;
    Document doc;
    String name;
    Integer id;
    private Hashtable<Integer,Object> matrixParseParams;
    private boolean isPersistent = true;
    public boolean isPersistent(){
        return isPersistent;
    }
    public void setPersistent(boolean pers){
        isPersistent = pers;
    }

    public Template(Document doc, String filename){
        this.filename = filename;
        id = tools.generateID();
        name = doc.getRootElement().getAttributeValue("name");
        this.doc = doc;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getFilename() {
        return filename;
    }

    public void setFilename(String filename) {
        this.filename = filename;
    }

    private static void initmmpkeys(){
        mppkeys = new Hashtable<String, Integer>();
        mppkeys.put("captionStringCount", Consts.CAPTION_STRING_COUNT);
        mppkeys.put("captionStringNum", Consts.CAPTION_STRING_NUM);

        mppkeys.put("multipleFile", Consts.MULTIPLE_FILE);
        mppkeys.put("matrixDelimeterColumn", Consts.MATRIX_DELIMETER_COLUMN);
        mppkeys.put("matrixDelimeterValue", Consts.MATRIX_DELIMETER_VALUE);
        mppkeys.put("matrixDelimeterType", Consts.MATRIX_DELIMETER_VALUE_TYPE);
        mppkeys.put("matrixDelimeterRep", Consts.MATRIX_DELIMETER_VALUE_REP);

        mppkeys.put("lineDilemeters", Consts.LINE_DELIMETER_SYMBOLS);
        mppkeys.put("useTabLineDilemeters", Consts.USE_TAB_LINE_DELIMETER_SYMBOLS);
        mppkeys.put("useSpaceLineDilemeters", Consts.USE_SPACE_LINE_DELIMETER_SYMBOLS);
    }

    public Hashtable<Integer,Object> getMatrixParserParams(){
        if (matrixParseParams!=null){
            return matrixParseParams;
        } else {
            matrixParseParams = new Hashtable<Integer, Object>();
        }
        if (mppkeys==null){
            initmmpkeys();
        }
        Hashtable<Integer,Object> answer = new Hashtable<Integer, Object>();
        Element parse_param = doc.getRootElement().getChild("parseParams");
        for (Object params:parse_param.getChildren("properties")){
            Element par = (Element)params;
            String name = par.getAttributeValue("name");
            //System.out.println(name);
            if (mppkeys.containsKey(name)){
                //System.out.println("    detected");
                if (par.getAttributeValue("type").equals("str")){
                    answer.put(mppkeys.get(name),par.getAttributeValue("value"));
                }
                if (par.getAttributeValue("type").equals("bool")){
                    answer.put(mppkeys.get(name),tools.parseBoolean(par.getAttributeValue("value")));
                }
                if (par.getAttributeValue("type").equals("int")){
                    answer.put(mppkeys.get(name),tools.parseInteger(par.getAttributeValue("value")));
                }
                if (par.getAttributeValue("type").equals("float")){
                    answer.put(mppkeys.get(name),tools.parseDouble(par.getAttributeValue("value")));
                }
            }

        }
        matrixParseParams = answer;
        return answer;
    }

    public List<String> getEntityTypes(){
        List children = doc.getRootElement().getChildren("entityParse");
        List<String> answer = new ArrayList<String>();
        for (Object o:children){
            Element el = (Element) o;
            answer.add(el.getAttributeValue("name"));
        }
        return answer;
    }

    public Element getEntityParseParams(String name){
        List children = doc.getRootElement().getChildren("entityParse");
        for (Object o:children){
            Element e = (Element) o;
            if (e.getAttributeValue("name").compareTo(name)==0) return e;
        }
        return null;
    }

}
